package com.budwk.app.sqfwV2.service.enterpriseWarrantyServe;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormSummaryService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.dto.EnterpriseWarrantyServeDTO;
import com.budwk.app.sqfwV2.enums.enterpriseWarrantyServe.EnterpriseWarrantyServeFileEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-25
 * <p>
 * 大营路社会治安 1
 */
@Slf4j
@IocBean
public class EnterpriseWarrantyServeExcelService extends SqfwTemplateExcelCommonService {

    protected final Map<String, String> mapFileIndex = new ConcurrentHashMap<>();

    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private SqfwFormSummaryService sqfwFormsummaryService;

    @Override
    public List<GenerateExcelUrlVO> generateExcel(List<ExcelCreateConfig> excelConfigList, String userId, List<String> sqfwIdList) {
        mapFileIndex.put(SecurityUtil.getUserId(), excelConfigList.get(0).getFileIdentity());
        return super.generateExcel(excelConfigList, userId, sqfwIdList);
    }

    @Override
    protected void customerPopularBaseExcelParam(BaseExcelParamDTO baseExcelParam, String userId, List<String> sqfwIdList) {

    }

    @Override
    protected void customerPopularBaseExcelParamForAdmin(BaseExcelParamDTO baseExcelParam, String userId, List<String> formSummaryNewIdList) {

    }

    @Override
    protected List<ExcelGeneratorConfig> getExcelCodeGeneratorConfig(List<ExcelCreateConfig> excelConfigList, BaseExcelParamDTO baseExcelParam) {
        List<ExcelGeneratorConfig> list = new ArrayList<>();
        for (ExcelCreateConfig config : excelConfigList) {
            EnterpriseWarrantyServeFileEnum enterpriseWarrantyServeFileByCode = EnterpriseWarrantyServeFileEnum.getEnterpriseWarrantyServeFileByCode(config.getFileIdentity());
            if (ObjectUtils.isNotEmpty(enterpriseWarrantyServeFileByCode)) {
                ExcelGeneratorConfig generatorConfig = new ExcelGeneratorConfig();
                generatorConfig.setGenerator(enterpriseWarrantyServeFileByCode.getServe());
                generatorConfig.setExcelCreateConfig(config);
                list.add(generatorConfig);
            }
        }
        return list;
    }

    @Override
    protected AbstractCustomerPdfCreateField parseParamData(String paramDataStr) {
        log.info("转换：" + JSONObject.parseObject(paramDataStr, EnterpriseWarrantyServeDTO.class));
        return JSONObject.parseObject(paramDataStr, EnterpriseWarrantyServeDTO.class);
    }

    @Override
    protected String getSqfwFormSummaryType() {
        return SupportBusinessTypeEnum.PUBLIC_SECURITY.getName();
    }

    protected List<Sqfw_formSummary_new> queryForSummaryData(List<String> sqfwIdList,Map<String,Object> param) {
        String fileIndex = mapFileIndex.get(SecurityUtil.getUserId());
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append(" select *,row_number() over(partition by shops_base_info_id  order by fill_date asc) as visitNum");
        sqlStr.append(" from  shequ.sqfw_form_summary_new sfsn");
        sqlStr.append(" where");
        sqlStr.append(" type ='").append(SupportBusinessTypeEnum.ENTERPRISE_WARRANTY_SERVE.getName()).append("'");
        sqlStr.append(" and sqfw_id in ").append(StrUtil.formatIds(sqfwIdList));
        if (EnterpriseWarrantyServeFileEnum.ENTERPRISE_APPEAL_BOOK.getCode().equals(fileIndex)) {
            sqlStr.append(" and param_data ->>'enterpriseAppeal' = 'true'");
        }
        sqlStr.append("  order by fill_date asc ");
        List<NutMap> nutMapList = sqfwFormsummaryService.listMap(Sqls.create(sqlStr.toString()));
        List<Sqfw_formSummary_new> sqfwFormSummaryNews = new ArrayList<>();
        for(NutMap nutMap:nutMapList){
            String paramData = nutMap.getString("param_data");
            Sqfw_formSummary_new sqfwFormSummaryNew = JSONObject.parseObject(JSON.toJSONString(nutMap),Sqfw_formSummary_new.class);
            sqfwFormSummaryNew.setParamData(paramData);
            sqfwFormSummaryNews.add(sqfwFormSummaryNew);
        }
        mapFileIndex.remove(SecurityUtil.getUserId());
        return sqfwFormSummaryNews;
    }


    @Override
    protected List<Sqfw_formSummary_new> queryForSummaryDataForAdmin(List<String> formSummaryNewIdList) {
        String fileIndex = mapFileIndex.get(SecurityUtil.getUserId());
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append(" select *,row_number() over(partition by shops_base_info_id  order by fill_date asc) as visitNum");
        sqlStr.append(" from  shequ.sqfw_form_summary_new sfsn");
        sqlStr.append(" where");
        sqlStr.append(" type ='").append(SupportBusinessTypeEnum.ENTERPRISE_WARRANTY_SERVE.getName()).append("'");
        sqlStr.append(" and id in ").append(StrUtil.formatIds(formSummaryNewIdList));
        if (EnterpriseWarrantyServeFileEnum.ENTERPRISE_APPEAL_BOOK.getCode().equals(fileIndex)) {
            sqlStr.append(" and param_data ->>'enterpriseAppeal' = 'true'");
        }
        sqlStr.append("  order by fill_date asc ");
        List<NutMap> nutMapList = sqfwFormsummaryService.listMap(Sqls.create(sqlStr.toString()));
        List<Sqfw_formSummary_new> sqfwFormSummaryNews = new ArrayList<>();
        for(NutMap nutMap:nutMapList){
            String paramData = nutMap.getString("param_data");
            Sqfw_formSummary_new sqfwFormSummaryNew = JSONObject.parseObject(JSON.toJSONString(nutMap),Sqfw_formSummary_new.class);
            sqfwFormSummaryNew.setParamData(paramData);
            sqfwFormSummaryNews.add(sqfwFormSummaryNew);
        }
        mapFileIndex.remove(SecurityUtil.getUserId());
        return sqfwFormSummaryNews;
    }
}
